﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace States
{
    public class GameplayState : BaseState
    {
        private static GameplayState theGameplayState;
      
        public SFX.ObjectManager theObjectManager;
        public static SFX.TextureManager theTextureManager;
        public SFX.SessionManager GameSession;

        UserInterface.UI GameUI;
        private GameplayState()
        {
            GameSession = new SFX.SessionManager();
        }

        public static GameplayState Instance
        {
            get
            {
                if (theGameplayState == null)
                    theGameplayState = new GameplayState();
                return theGameplayState;
            }
        }

        public override void Enter()
        {
            theObjectManager = new SFX.ObjectManager(theSpriteBatch);
            theTextureManager = new SFX.TextureManager(theContentManager);
            LoadGameTextures();
            LoadEnvironmentObjects();
            GameSession.InitializeTanks();
            GameUI = new UserInterface.UI();
        }
        public void LoadEnvironmentObjects()
        {
            //Objects.EnvironmentObject Rock = new Objects.EnvironmentObject();
            //theObjectManager.AddObject(Rock);
        }
        public void LoadGameTextures()
        {

            theTextureManager.AddTexture("BACKGROUND", "Backgrounds/Background");

            theTextureManager.AddTexture("BULLET", "Objects/Bullet");
            theTextureManager.AddFont("DEBUG", "Fonts/DebugFont");

            //  Tank Assets
            theTextureManager.AddTexture("TANK1", "Objects/Tanks/player1bot");
            theTextureManager.AddTexture("TANK2", "Objects/Tanks/player2bot");
            theTextureManager.AddTexture("TANK3", "Objects/Tanks/player3bot");
            theTextureManager.AddTexture("TANK4", "Objects/Tanks/player4bot");

            theTextureManager.AddTexture("TANK1TURRET", "Objects/Tanks/player1top");
            theTextureManager.AddTexture("TANK2TURRET", "Objects/Tanks/player2top");
            theTextureManager.AddTexture("TANK3TURRET", "Objects/Tanks/player3top");
            theTextureManager.AddTexture("TANK4TURRET", "Objects/Tanks/player4top");

            theTextureManager.AddTexture("BOX", "Objects/Environment/Box");

        }
        public override void Exit()
        {
            theObjectManager.Shutdown();
            theTextureManager.ShutdownTextureManager();
            GameUI = null;
        }

        public override void GetInput(float ElapsedTime)
        {
            
        }

        public override void Update(float ElapsedTime)
        {
            GetInput(ElapsedTime);
            theObjectManager.Update(ElapsedTime);
            GameSession.UpdatePlayers(ElapsedTime);
            SFX.AnimatedEffects.AnimatedEffectsInstance.Update(ElapsedTime);
            GameUI.Update(ElapsedTime);
        }
        public override void Render(float ElapsedTime)
        {
            theDevice.Clear(Color.White);
            theSpriteBatch.Begin();
            theSpriteBatch.Draw(theTextureManager.GetTexture("BACKGROUND"), new Vector2(0, 0), Color.White);
            GameSession.RenderPlayers(ElapsedTime);
            theObjectManager.Render(ElapsedTime);
            SFX.AnimatedEffects.AnimatedEffectsInstance.Render(ElapsedTime);
            GameUI.Render(ElapsedTime);
            theSpriteBatch.End();
        }
    }
}